package com.itjeffrey.autocode.controller;

import com.itjeffrey.autocode.bo.UserInfoBO;
import com.itjeffrey.autocode.bo.req.IsEnableBO;
import com.itjeffrey.autocode.bo.req.RegisterBO;
import com.itjeffrey.autocode.bo.req.RelateRoleBO;
import com.itjeffrey.autocode.common.Result;
import com.itjeffrey.autocode.service.LoginService;
import com.itjeffrey.autocode.service.UserService;
import com.itjeffrey.autocode.bo.req.ModPwdBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 用户控制器
 *
 * @From: Jeffrey
 * @Date: 2020/11/30
 */
@Api(value = "UserController") //描述Controller，可做Swagger2扫描类依据
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController implements BaseUploadController<MultipartFile, String> {

    @Resource
    private LoginService loginService;
    @Resource
    private UserService userService;

    /**
     * 注册
     *
     * @param registerBO
     * @return
     */
    @ApiOperation(value = "用户注册")
    @ApiImplicitParam(name = "registerBO", value = "用户注册BO", required = true, dataType = "RegisterBO", paramType = "注册")
    @ApiResponse(code = 200, message = "用户注册成功")
    @PostMapping("/register")
    public ResponseEntity<Result> register(@RequestBody @Valid RegisterBO registerBO) {
        return ResponseEntity.ok(loginService.register(registerBO));
    }

    /**
     * 登录
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<Result> login(HttpServletRequest request, HttpServletResponse response) {
        return ResponseEntity.ok(loginService.login(request.getParameter("username"), request.getParameter
                ("password")));
    }

    /**
     * 登出
     *
     * @return
     */
    @PostMapping("/logout")
    public ResponseEntity<Result> logout(HttpServletRequest request, HttpServletResponse response) {
        return ResponseEntity.ok(loginService.logout(request, response));
    }

    /**
     * 刷新token
     *
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/refreshToken")
    public ResponseEntity<Result> refreshToken(HttpServletRequest request, HttpServletResponse response) {
        return ResponseEntity.ok(loginService.refreshToken(request, response));
    }

    /**
     * 修改用户信息
     * @param userInfoBO
     * @return
     */
    @PostMapping("/mod")
    public ResponseEntity<Result> modUser(@RequestBody UserInfoBO userInfoBO){
        return ResponseEntity.ok(userService.modUser(userInfoBO));
    }

    /**
     * 修改密码
     *
     * @param modPwdBO
     * @return
     */
    @PostMapping("/modPwd")
    public ResponseEntity<Result> modPwd(@RequestBody @Validated ModPwdBO modPwdBO) {
        return ResponseEntity.ok(loginService.modPwd(modPwdBO));
    }

    /**
     * 删除用户信息
     *
     * @param loginname
     * @return
     */
    @PostMapping("/delete/{loginname}")
    public ResponseEntity<Result> delete(@PathVariable("loginname") String loginname) {
        return ResponseEntity.ok(userService.delete(loginname));
    }

    /**
     * 用户头像上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    @PostMapping("/avater/upload")
    public ResponseEntity<Result<String>> upload(MultipartFile multipartFile) {
        return ResponseEntity.ok(userService.uploadAvater(multipartFile));
    }

    /**
     * 根据登录名获取用户头像url
     * //     * @param loginName
     *
     * @return
     */
    @PostMapping("/getAvaUrl")
    public ResponseEntity<Result> getAvaUrl() {
        return ResponseEntity.ok(userService.getAvaUrl());
    }

    /**
     * 根据用户名查询用户信息
     * @return
     */
    @PostMapping("/query/{loginname}")
    public ResponseEntity<Result> queryOne(@PathVariable("loginname") String loginname){
        return ResponseEntity.ok(userService.queryOne(loginname));
    }

    /**
     * 查询用户列表
     * @return
     */
    @PostMapping("/queryList")
    public ResponseEntity<Result> queryList(@RequestBody UserInfoBO userInfoBO){
        return ResponseEntity.ok(userService.queryList(userInfoBO));
    }

    /**
     * 用户是否启用
     * @param isEnableBO
     * @return
     */
    @PostMapping("/isEnable")
    public ResponseEntity<Result> isEnable(@RequestBody IsEnableBO isEnableBO){
        return ResponseEntity.ok(userService.isEnable(isEnableBO));
    }

    /**
     * 用户关联角色
     * @param relateRoleBO
     * @return
     */
    @PostMapping("/relateRole")
    public ResponseEntity<Result> relateRole(@RequestBody RelateRoleBO relateRoleBO){
        return ResponseEntity.ok(userService.relateRole(relateRoleBO));
    }
}
